package org.jspar.place;

import org.jspar.tile.DefaultTileDelegate;
import org.jspar.tile.ITileContent;
import org.jspar.tile.Tile;

public class StringContentsTileDelegate extends DefaultTileDelegate {

	@Override
	public void insertContents(Tile tile, ITileContent what) {
		ModuleString ml = (ModuleString) what;
		for(int i = 0; i < ml.length(); ++i) {
			ml.moduleAt(i).setHTile(tile);
		}
		super.insertContents(tile, what);
	}
	
	@Override
	public void manageContentsHmerge(Tile newTile, Tile oldTile) {
		manageContentsForMerge(oldTile, newTile);
	}

	@Override
	public void manageContentsVmerge(Tile newTile, Tile oldTile) {
		manageContentsForMerge(newTile, oldTile);
	}
	
	private void manageContentsForMerge(Tile newTile, Tile oldTile) {
		ModuleString ml = (ModuleString) oldTile.content();
		if (ml != null) {
			for (int i = 0; i < ml.length(); ++i) {
				ml.moduleAt(i).setHTile(newTile);
			}
			newTile.setContent(ml);
		}
	}
}
